package cn.zwx.activiti.example.controller;

import cn.hutool.core.util.StrUtil;
import cn.zwx.activiti.example.common.ResponseEntry;
import cn.zwx.activiti.example.common.enums.ErrorEnum;
import cn.zwx.activiti.example.common.exception.BizException;
import cn.zwx.activiti.example.model.req.InstancesByUserReq;
import cn.zwx.activiti.example.model.resp.InstancesByIdResp;
import cn.zwx.activiti.example.model.resp.InstancesByUserResp;
import cn.zwx.activiti.example.service.ActivitiHistoryService;
import cn.zwx.activiti.example.utils.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.activiti.api.runtime.shared.query.Page;
import org.activiti.engine.history.HistoricTaskInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: 用户任务历史查看
 * @projectName:activiti-example
 * @see:cn.zwx.activiti.example.controller
 * @author:zhangwenxue
 * @createTime:2021/6/8 20:25
 * @version:1.0
 */
@Api(tags = "用户任务历史查看")
@RestController
@RequestMapping("/activitiHistory")
public class ActivitiHistoryController extends BaseController{

    @Autowired
    private ActivitiHistoryService activitiHistoryService;

    @Autowired
    private SecurityUtil securityUtil;

    /**
     * @description 查询用户历史任务
     * @return cn.zwx.activiti.example.common.ResponseEntry<org.activiti.api.runtime.shared.query.Page<cn.zwx.activiti.example.model.resp.InstancesByUserResp>>
     * @author zhangwenxue
     * @createTime 2021/6/8 20:30
     **/
    @ApiOperation("查询用户历史任务")
    @RequestMapping(value = "/getInstancesByUserName",method = RequestMethod.POST)
    public ResponseEntry<Page<InstancesByUserResp>> getInstancesByUser(@RequestBody InstancesByUserReq req) {
        // 校验参数
        securityUtil.logInAs(getUserName());
        // 请求接口
        Page<InstancesByUserResp> page = activitiHistoryService.getInstancesByUser(getUserName(),req);
        // 返回参数
        return ResponseEntry.ok(page);
    }

    /**
     * @description 根据流程实例id任务历史
     * @return AjaxResponse
     * @author zhangwenxue
     * @createTime 2021/6/8 20:46
     **/
    @RequestMapping(value = "/getInstancesById",method = RequestMethod.GET)
    @ApiOperation("根据流程实例id任务历史")
    public ResponseEntry<List<InstancesByIdResp>> getInstancesById(@RequestParam("processInstanceId") String processInstanceId) {
        // 校验参数
        if (StrUtil.isBlank(processInstanceId)){
            throw new BizException(ErrorEnum.PROCESS_INSTANCE_ID_IS_EMPTY);
        }
        securityUtil.logInAs(getUserName());
        // 请求接口
        List<InstancesByIdResp> list =  activitiHistoryService.getInstancesById(processInstanceId);
        // 返回参数
        return ResponseEntry.ok(list);
     }


}
